import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'dart:convert';
import 'dlt.dart';
import 'ssq.dart';

class Lottery extends StatefulWidget {
  @override
  _LotteryState createState() => _LotteryState();
}

final String _key = "51fd02768476bfa6805e778dcd91220b";

class _LotteryState extends State<Lottery> {
  ssq _ssqResult;
  dlt _dltResult;

  @override
  void initState() {
    super.initState();
    Future futuredlt = new Future(() => dlt);
    Future futuressq = new Future(() => ssq);
    futuredlt.then((_) async {
      try {
        Response response = await Dio().post(
            "http://apis.juhe.cn/lottery/query?key=" +
                _key +
                "&lottery_id=dlt&lottery_no=");
        Map result = json.decode(response.toString());
        print(result.toString());
        setState(() {
          _dltResult = dlt.fromJson(result);
        });
      } catch (e) {
        print(e.toString());
      }
    }).whenComplete(() {
      futuressq.then((_) async {
        try {
          Response response = await Dio().post(
              "http://apis.juhe.cn/lottery/query?key=" +
                  _key +
                  "&lottery_id=ssq&lottery_no=");
          Map result = json.decode(response.toString());
          print(result.toString());
          setState(() {
            _ssqResult = ssq.fromJson(result);
          });
        } catch (e) {
          print(e.toString());
        }
      }).catchError((_) {
        print('异常');
      });
    });


  }

  @override
  Widget build(BuildContext context) {
      if(_ssqResult == null || _dltResult == null){
        return Container(
          child: Text('正在加载数据...'),
        );
      }else {
        return Container(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Padding(
                  padding: EdgeInsets.fromLTRB(10, 10, 0, 10),
                  child: Text(
                    "最新开奖结果",
                    style: TextStyle(
                        fontSize: ScreenUtil().setSp(48),
                        fontWeight: FontWeight.bold,
                        color: Colors.red),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.fromLTRB(10, 10, 0, 10),
                  child: Text(
                    _ssqResult.result.lotteryName +
                        " 第" +
                        _ssqResult.result.lotteryNo +
                        "期",
                    style: TextStyle(
                        fontSize: ScreenUtil().setSp(48),
                        fontWeight: FontWeight.bold,
                        color: Colors.blue),
                  ),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    FlatButton(
                      child: Text(
                        _ssqResult.result.lotteryRes.split(',')[0],
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: ScreenUtil().setSp(48),
                            fontWeight: FontWeight.bold),
                      ),
                      color: Colors.red,
                      minWidth: ScreenUtil().setWidth(50),
                      textColor: Colors.white,
                      disabledColor: Colors.red,
                      shape: CircleBorder(
                        side: BorderSide(
                          color: Colors.red,
                        ),
                      ),
                    ),
                    FlatButton(
                      child: Text(
                        _ssqResult.result.lotteryRes.split(',')[1],
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: ScreenUtil().setSp(48),
                            fontWeight: FontWeight.bold),
                      ),
                      color: Colors.red,
                      textColor: Colors.white,
                      minWidth: ScreenUtil().setWidth(50),
                      disabledColor: Colors.red,
                      shape: CircleBorder(
                        side: BorderSide(
                          color: Colors.red,
                        ),
                      ),
                    ),
                    FlatButton(
                      child: Text(
                        _ssqResult.result.lotteryRes.split(',')[2],
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: ScreenUtil().setSp(48),
                            fontWeight: FontWeight.bold),
                      ),
                      color: Colors.red,
                      textColor: Colors.white,
                      minWidth: ScreenUtil().setWidth(50),
                      disabledColor: Colors.red,
                      shape: CircleBorder(
                        side: BorderSide(
                          color: Colors.red,
                        ),
                      ),
                    ),
                    FlatButton(
                      child: Text(
                        _ssqResult.result.lotteryRes.split(',')[3],
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: ScreenUtil().setSp(48),
                            fontWeight: FontWeight.bold),
                      ),
                      color: Colors.red,
                      textColor: Colors.white,
                      disabledColor: Colors.red,
                      minWidth: ScreenUtil().setWidth(50),
                      shape: CircleBorder(
                        side: BorderSide(
                          color: Colors.red,
                        ),
                      ),
                    ),
                    FlatButton(
                      child: Text(
                        _ssqResult.result.lotteryRes.split(',')[4],
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: ScreenUtil().setSp(48),
                            fontWeight: FontWeight.bold),
                      ),
                      color: Colors.red,
                      textColor: Colors.white,
                      disabledColor: Colors.red,
                      minWidth: ScreenUtil().setWidth(50),
                      shape: CircleBorder(
                        side: BorderSide(
                          color: Colors.red,
                        ),
                      ),
                    ),
                    FlatButton(
                      child: Text(
                        _ssqResult.result.lotteryRes.split(',')[5],
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: ScreenUtil().setSp(48),
                            fontWeight: FontWeight.bold),
                      ),
                      color: Colors.red,
                      textColor: Colors.white,
                      minWidth: ScreenUtil().setWidth(50),
                      disabledColor: Colors.red,
                      shape: CircleBorder(
                        side: BorderSide(
                          color: Colors.red,
                        ),
                      ),
                    ),
                    FlatButton(
                      child: Text(
                        _ssqResult.result.lotteryRes.split(',')[6],
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: ScreenUtil().setSp(48),
                            fontWeight: FontWeight.bold),
                      ),
                      color: Colors.blue,
                      textColor: Colors.white,
                      disabledColor: Colors.blue,
                      minWidth: ScreenUtil().setWidth(50),
                      shape: CircleBorder(
                        side: BorderSide(
                          color: Colors.blue,
                        ),
                      ),
                    ),
                  ],
                ),
                Padding(
                  padding: EdgeInsets.fromLTRB(10, 10, 0, 10),
                  child: Text(
                    _dltResult.result.lotteryName +
                        " 第" +
                        _dltResult.result.lotteryNo +
                        "期",
                    style: TextStyle(
                        fontSize: ScreenUtil().setSp(48),
                        fontWeight: FontWeight.bold,
                        color: Colors.blue),
                  ),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    FlatButton(
                      child: Text(
                        _dltResult.result.lotteryRes.split(',')[0],
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: ScreenUtil().setSp(48),
                            fontWeight: FontWeight.bold),
                      ),
                      color: Colors.red,
                      minWidth: ScreenUtil().setWidth(50),
                      textColor: Colors.white,
                      disabledColor: Colors.red,
                      shape: CircleBorder(
                        side: BorderSide(
                          color: Colors.red,
                        ),
                      ),
                    ),
                    FlatButton(
                      child: Text(
                        _dltResult.result.lotteryRes.split(',')[1],
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: ScreenUtil().setSp(48),
                            fontWeight: FontWeight.bold),
                      ),
                      color: Colors.red,
                      textColor: Colors.white,
                      minWidth: ScreenUtil().setWidth(50),
                      disabledColor: Colors.red,
                      shape: CircleBorder(
                        side: BorderSide(
                          color: Colors.red,
                        ),
                      ),
                    ),
                    FlatButton(
                      child: Text(
                        _dltResult.result.lotteryRes.split(',')[2],
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: ScreenUtil().setSp(48),
                            fontWeight: FontWeight.bold),
                      ),
                      color: Colors.red,
                      textColor: Colors.white,
                      minWidth: ScreenUtil().setWidth(50),
                      disabledColor: Colors.red,
                      shape: CircleBorder(
                        side: BorderSide(
                          color: Colors.red,
                        ),
                      ),
                    ),
                    FlatButton(
                      child: Text(
                        _dltResult.result.lotteryRes.split(',')[3],
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: ScreenUtil().setSp(48),
                            fontWeight: FontWeight.bold),
                      ),
                      color: Colors.red,
                      textColor: Colors.white,
                      disabledColor: Colors.red,
                      minWidth: ScreenUtil().setWidth(50),
                      shape: CircleBorder(
                        side: BorderSide(
                          color: Colors.red,
                        ),
                      ),
                    ),
                    FlatButton(
                      child: Text(
                        _dltResult.result.lotteryRes.split(',')[4],
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: ScreenUtil().setSp(48),
                            fontWeight: FontWeight.bold),
                      ),
                      color: Colors.red,
                      textColor: Colors.white,
                      disabledColor: Colors.red,
                      minWidth: ScreenUtil().setWidth(50),
                      shape: CircleBorder(
                        side: BorderSide(
                          color: Colors.red,
                        ),
                      ),
                    ),
                    FlatButton(
                      child: Text(
                        _dltResult.result.lotteryRes.split(',')[5],
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: ScreenUtil().setSp(48),
                            fontWeight: FontWeight.bold),
                      ),
                      color: Colors.blue,
                      textColor: Colors.white,
                      minWidth: ScreenUtil().setWidth(50),
                      disabledColor: Colors.blue,
                      shape: CircleBorder(
                        side: BorderSide(
                          color: Colors.blue,
                        ),
                      ),
                    ),
                    FlatButton(
                      child: Text(
                        _dltResult.result.lotteryRes.split(',')[6],
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: ScreenUtil().setSp(48),
                            fontWeight: FontWeight.bold),
                      ),
                      color: Colors.blue,
                      textColor: Colors.white,
                      disabledColor: Colors.blue,
                      minWidth: ScreenUtil().setWidth(50),
                      shape: CircleBorder(
                        side: BorderSide(
                          color: Colors.blue,
                        ),
                      ),
                    ),
                  ],
                ),
                Padding(
                  padding: EdgeInsets.fromLTRB(10, 10, 0, 10),
                  child: Text(
                    "双色球累计奖池奖金:",
                    style: TextStyle(
                        fontSize: ScreenUtil().setSp(48),
                        fontWeight: FontWeight.bold),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.fromLTRB(10, 10, 0, 10),
                  child: Text(
                    _ssqResult.result.lotteryPoolAmount + " 元",
                    style: TextStyle(
                        fontSize: ScreenUtil().setSp(100),
                        fontWeight: FontWeight.bold,
                        color: Colors.red),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.fromLTRB(10, 10, 0, 10),
                  child: Text(
                    "大乐透累计奖池奖金:",
                    style: TextStyle(
                        fontSize: ScreenUtil().setSp(48),
                        fontWeight: FontWeight.bold),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.fromLTRB(10, 10, 0, 10),
                  child: Text(
                    _dltResult.result.lotteryPoolAmount + " 元",
                    style: TextStyle(
                        fontSize: ScreenUtil().setSp(100),
                        fontWeight: FontWeight.bold,
                        color: Colors.red),
                  ),
                ),
              ],
            ));
      }
  }
}
